package com.jcsz.gulimall.cart.controller;

import com.jcsz.common.constant.CartConstant;
import com.jcsz.gulimall.cart.interceptor.CartInterceptor;
import com.jcsz.gulimall.cart.service.CartService;
import com.jcsz.gulimall.cart.vo.Cart;
import com.jcsz.gulimall.cart.to.UserInfoTo;
import com.jcsz.gulimall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @className: CartController
 * @author: 一把Po吉他
 * @date: 2022/11/10 12:03
 * @version: 1.0
 **/
@Controller
public class CartController {
    @Autowired
    CartService cartService;


    @GetMapping("/cart.html")
    public String cartPage(Model model) throws ExecutionException, InterruptedException {
        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
        System.out.println(userInfoTo);
        Cart cart = cartService.getCart();
        model.addAttribute("cartList", cart);
        return "cartList";
    }

    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId, num);
        //model.addAttribute("cartItem", cartItem);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model) throws ExecutionException, InterruptedException {
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("cartItem", cartItem);
        return "success";
    }

    @PostMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("isChecked") Integer isChecked) {
        cartService.checkItem(skuId, isChecked);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    //countItem
    @PostMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
        cartService.countItem(skuId, num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    //deleteItem
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/getCurrentUserCartItem")
    @ResponseBody
    public List<CartItem> getCurrentUserCartItem() {
        return cartService.getCurrentUserCartItem();
    }
}
